import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'


import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite' 
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import styleImport from 'vite-plugin-style-import'

import path from "path"

// console.log(resolve)
// https://vitejs.dev/config/
export default defineConfig({
    server: {
        host: true,
        port: 2021,
        open: true,
        proxy: {
            // 字符串简写方式
            // '/api': 'http://localhost:9000',
            // 选项写法
            '/api': {
                target: 'http://192.168.222.666:8095', // 所要代理的目标地址
                rewrite: path => path.replace(/^\/api/, ''), // 重写传过来的path路径，比如 `/api/index/1?id=10&name=zs`（注意:path路径最前面有斜杠（/），因此，正则匹配的时候不要忘了是斜杠（/）开头的；选项的 key 也是斜杠（/）开头的）
                changeOrigin: true // true/false, Default: false - changes the origin of the host header to the target URL
            }
        }
    },

    resolve: {
        alias: {
            "@": path.resolve(__dirname, "src"),
            "~": path.resolve(__dirname, "src/components"),
            "^": path.resolve(__dirname, "src/api")
        }
    },
    plugins: [
        vue(),
        AutoImport({
            resolvers: [ElementPlusResolver()]
        }),
        // 初次启动批量导入会出现 ✨ dependencies updated, reloading page
        Components({
            resolvers: [ElementPlusResolver()]
        }),

        styleImport({
            libs: [{
                libraryName: 'element-plus',
                esModule: true,
                resolveStyle: (name) => {
                    return `element-plus/theme-chalk/${name}.css`
                }
            }]
        })
    ],
  
    css: {
        preprocessorOptions: {
            scss: {
                charset: false,
                additionalData: `@import "./src/style/variables.scss";`
            }
        }
    },

    build: {
        assetsDir: './static',
        // terserOptions: {
        //   compress: {
        //     dead_code: true, //删除无法访问的代码
        //     drop_debugger: true, //  删除debugger;语句
        //     drop_console: true, //放弃对console.*函数的调用
        //     // pure_funcs: ['console.info', 'console.error', 'console.warn']
        //     pure_funcs: ['console.log']
        //   }
        // },
        rollupOptions: {
            output: {
                manualChunks (id) {
                    if (id.includes('node_modules')) {
                        return id.toString().split('node_modules/')[1].split('/')[0].toString()
                    }
                }
            }
        }
    }
})